export default {
    data() {
        return {
            // 修改弹出框
            dialogUpd: false,
            // 所有商品数据
            goodsList: [],
            // 请求数据参数
            queryInfo: {
                query: '',
                pagenum: 1,
                pagesize: 5
            },
            // 总数据
            total: 0,
            // 修改商品数据
            updateGoodsForm: {
                goods_name: '',
                goods_price: '',
                goods_number: '',
                goods_weight: ''
            },
            // 当前商品的id
            goodsId: '',
            // 校验规则
            rules: {
                goods_name: [{
                    required: true,
                    message: '请输入商品名称',
                    trigger: 'blur'
                }, ],
                goods_price: [{
                    required: true,
                    message: '请输入商品价格',
                    trigger: 'blur'
                }, ],
                goods_weight: [{
                    required: true,
                    message: '请输入商品重量',
                    trigger: 'blur'
                }, ],
                goods_number: [{
                    required: true,
                    message: '请输入商品数量',
                    trigger: 'blur'
                }, ],
                goods_cat: [{
                    required: true,
                    message: '请输入商品名称',
                    trigger: 'blur'
                }, ]
            }
        }
    },
    created() {
        this.getGoodsList();
    },
    methods: {
        // 获取所有商品数据
        async getGoodsList() {
            let {
                data: res
            } = await this.$http.get('goods', {
                params: this.queryInfo
            });
            if (res.meta.status != 200) return this.$message.error('获取商品列表数据失败');
            this.goodsList = res.data.goods;
            this.total = res.data.total;
        },
        // 分页方法
        handleSizeChange(val) {
            // console.log(`每页 ${val} 条`);
            this.queryInfo.pagesize = val;
            this.getGoodsList();
        },
        handleCurrentChange(val) {
            // console.log(`当前页: ${val}`);
            this.queryInfo.pagenum = val;
            this.getGoodsList();
        },
        // 点击跳转到添加商品页面
        clickSkip() {
            this.$router.push('/home/goodsAdd');
        },
        // 根据 ID 查询商品
        async searchGoods(id) {
            this.goodsId = id;
            this.dialogUpd = true;
            let {
                data: res
            } = await this.$http.get('goods/' + id);
            // console.log(res)
            if (res.meta.status != 200) return this.$message.error('查询商品失败');
            this.updateGoodsForm = res.data;
        },
        // 修改商品
        updateGoods() {
            this.$refs.updateGoodsForm.validate(async valid => {
                if (!valid) return;
                let {
                    data: res
                } = await this.$http.put('goods/' + this.goodsId, this.updateGoodsForm);
                // console.log(res)
                if (res.meta.status != 200) return this.$message.error('修改商品失败');
                this.dialogUpd = false;
                this.getGoodsList();
                this.$message.success('修改商品成功');
            })
        },
        // 删除商品
        async deleteGoods(id) {
            let confirm = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch(error => error);
            if (confirm == 'cancel') return this.$message('取消删除成功');
            let {
                data: res
            } = await this.$http.delete('goods/' + id);
            // console.log(res);
            if (res.meta.status != 200) return this.$message.error('删除失败');
            this.getGoodsList();
            this.$message.success('删除成功');
        }
    },
}